﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Drawing;
using fideeraSoft.Utils;
using System.Windows.Forms;
using fideeraSoft.Entities;

namespace fideeraSoft.Paneles
{
    public class MateriaPrimaPanelAlta:PanelGeneral
    {
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.TableLayoutPanel tlpGral;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.TextBox tbDescripcion;
        private System.Windows.Forms.Button btSubmit;
        private System.Windows.Forms.TextBox tbNombre;
        private System.Windows.Forms.GroupBox gbGral;
        private System.Windows.Forms.Label lbNombre;
        private System.Windows.Forms.Label lbId;
        private System.Windows.Forms.Label lbDescripcion;
        private System.Windows.Forms.Label lbIdMateriaPrima;

        private Label lbCaracteristica;
        private TextBox tbCaracteristicas;
        private Label lbUnidad;
        private TextBox tbUnidad;
        private Label lbCodigo;
        private TextBox tbCodigo;
        private int posicionTablaBotones;

        public MateriaPrimaPanelAlta(Handler handler)
        {
            this.formatearPanelGeneral(handler);

            InitializeComponent();

            this.formatearTableTitulo("ALTA DE MATERIA PRIMA / INSUMO");

            
            this.formatearGroupBox();
            this.formatTableForm();
            this.formatearTablaBotones();

            lbIdMateriaPrima.Text = this.handler.buscarNuevoUltimoIdMateriaPrimaEnBD().ToString();

            
            this.Controls.Add(gbGral);
            this.Controls.Add(tlpBotones);

        
        }

        #region FORMATEO
        private void formatearGroupBox()
        {
            
            gbGral.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, gbGral.Size.Width), this.InitialRealPointVertical);

            gbGral.Controls.Add(tlpGral);
        }

        
        private void formatTableForm()
        {
            //tlpForm.Size = new Size(gbAltaProveedor.Width-10,gbAltaProveedor.Height-300);

            tlpGral.ColumnStyles.Clear();
            for (int i = 0; i < tlpGral.ColumnCount; i++)
            {
                tlpGral.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            }

            tlpGral.RowStyles.Clear();
            for (int i = 0; i < tlpGral.RowCount; i++)
            {
                tlpGral.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }

            //tlpForm.Size = new Size((gbAltaProveedor.Size.Width - 10));

            tlpGral.BackColor = Color.LightCyan;
            tlpGral.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpGral.Location = new Point(MetodosGenerales.calcularCenterWidth(gbGral.Size.Width, tlpGral.Size.Width), 30);

            cargarControlesEnTabla();

        }
        
  


        private void formatearTablaBotones()
        {            
            posicionTablaBotones = this.InitialRealPointVertical + gbGral.Size.Height + 20;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, tlpBotones.Size.Width), posicionTablaBotones);

            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btSubmit);

        }

        #endregion

        private void cargarControlesEnTabla()
        {

            tlpGral.Controls.Add(lbId);
            tlpGral.Controls.Add(lbIdMateriaPrima);

            tlpGral.Controls.Add(lbNombre);
            tlpGral.Controls.Add(tbNombre);

            tlpGral.Controls.Add(lbCodigo);
            tlpGral.Controls.Add(tbCodigo);

            tlpGral.Controls.Add(lbDescripcion);
            tlpGral.Controls.Add(tbDescripcion);

            tlpGral.Controls.Add(lbUnidad);
            tlpGral.Controls.Add(tbUnidad);

            tlpGral.Controls.Add(lbCaracteristica);
            tlpGral.Controls.Add(tbCaracteristicas);
                       

        }

        private void InitializeComponent()
        {
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.tlpGral = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            this.tbDescripcion = new System.Windows.Forms.TextBox();
            this.btSubmit = new System.Windows.Forms.Button();
            this.tbNombre = new System.Windows.Forms.TextBox();
            this.gbGral = new System.Windows.Forms.GroupBox();
            this.lbNombre = new System.Windows.Forms.Label();
            this.lbId = new System.Windows.Forms.Label();
            this.lbDescripcion = new System.Windows.Forms.Label();
            this.lbIdMateriaPrima = new System.Windows.Forms.Label();
            this.lbCaracteristica = new System.Windows.Forms.Label();
            this.tbCaracteristicas = new System.Windows.Forms.TextBox();
            this.lbUnidad = new System.Windows.Forms.Label();
            this.tbUnidad = new System.Windows.Forms.TextBox();
            this.lbCodigo = new System.Windows.Forms.Label();
            this.tbCodigo = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // tlpGral
            // 
            this.tlpGral.ColumnCount = 2;
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.Location = new System.Drawing.Point(0, 0);
            this.tlpGral.Name = "tlpGral";
            this.tlpGral.RowCount = 7;
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.Size = new System.Drawing.Size(700, 300);
            this.tlpGral.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // tbDescripcion
            // 
            this.tbDescripcion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbDescripcion.Location = new System.Drawing.Point(0, 0);
            this.tbDescripcion.MaxLength = 150;
            this.tbDescripcion.Name = "tbDescripcion";
            this.tbDescripcion.Size = new System.Drawing.Size(470, 30);
            this.tbDescripcion.TabIndex = 0;
            // 
            // btSubmit
            // 
            this.btSubmit.BackColor = System.Drawing.Color.AliceBlue;
            this.btSubmit.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btSubmit.Location = new System.Drawing.Point(0, 0);
            this.btSubmit.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btSubmit.Name = "btSubmit";
            this.btSubmit.Size = new System.Drawing.Size(75, 40);
            this.btSubmit.TabIndex = 0;
            this.btSubmit.Text = "Guardar";
            this.btSubmit.UseVisualStyleBackColor = false;
            this.btSubmit.Click += new System.EventHandler(this.btSubmit_Click);
            // 
            // tbNombre
            // 
            this.tbNombre.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbNombre.Location = new System.Drawing.Point(0, 0);
            this.tbNombre.MaxLength = 50;
            this.tbNombre.Name = "tbNombre";
            this.tbNombre.Size = new System.Drawing.Size(250, 30);
            this.tbNombre.TabIndex = 0;
            // 
            // gbGral
            // 
            this.gbGral.Font = new System.Drawing.Font("Bodoni MT", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.gbGral.Location = new System.Drawing.Point(0, 0);
            this.gbGral.Name = "gbGral";
            this.gbGral.Size = new System.Drawing.Size(800, 400);
            this.gbGral.TabIndex = 0;
            this.gbGral.TabStop = false;
            this.gbGral.Text = "Alta de Materia Prima / Insumo";
            // 
            // lbNombre
            // 
            this.lbNombre.AutoSize = true;
            this.lbNombre.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbNombre.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbNombre.Location = new System.Drawing.Point(0, 0);
            this.lbNombre.Name = "lbNombre";
            this.lbNombre.Size = new System.Drawing.Size(100, 23);
            this.lbNombre.TabIndex = 0;
            this.lbNombre.Text = "Nombre";
            this.lbNombre.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbId
            // 
            this.lbId.AutoSize = true;
            this.lbId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbId.Location = new System.Drawing.Point(0, 0);
            this.lbId.Name = "lbId";
            this.lbId.Size = new System.Drawing.Size(100, 23);
            this.lbId.TabIndex = 0;
            this.lbId.Text = "ID de Materia Prima";
            this.lbId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbDescripcion
            // 
            this.lbDescripcion.AutoSize = true;
            this.lbDescripcion.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbDescripcion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbDescripcion.Location = new System.Drawing.Point(0, 0);
            this.lbDescripcion.Name = "lbDescripcion";
            this.lbDescripcion.Size = new System.Drawing.Size(100, 23);
            this.lbDescripcion.TabIndex = 0;
            this.lbDescripcion.Text = "Descripción";
            this.lbDescripcion.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbIdMateriaPrima
            // 
            this.lbIdMateriaPrima.AutoSize = true;
            this.lbIdMateriaPrima.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbIdMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbIdMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.lbIdMateriaPrima.Name = "lbIdMateriaPrima";
            this.lbIdMateriaPrima.Size = new System.Drawing.Size(100, 23);
            this.lbIdMateriaPrima.TabIndex = 0;
            this.lbIdMateriaPrima.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lbCaracteristica
            // 
            this.lbCaracteristica.AutoSize = true;
            this.lbCaracteristica.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCaracteristica.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCaracteristica.Location = new System.Drawing.Point(0, 0);
            this.lbCaracteristica.Name = "lbCaracteristica";
            this.lbCaracteristica.Size = new System.Drawing.Size(100, 23);
            this.lbCaracteristica.TabIndex = 0;
            this.lbCaracteristica.Text = "Características";
            this.lbCaracteristica.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbCaracteristicas
            // 
            this.tbCaracteristicas.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCaracteristicas.Location = new System.Drawing.Point(0, 0);
            this.tbCaracteristicas.MaxLength = 50;
            this.tbCaracteristicas.Name = "tbCaracteristicas";
            this.tbCaracteristicas.Size = new System.Drawing.Size(470, 30);
            this.tbCaracteristicas.TabIndex = 0;
            // 
            // lbUnidad
            // 
            this.lbUnidad.AutoSize = true;
            this.lbUnidad.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbUnidad.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbUnidad.Location = new System.Drawing.Point(0, 0);
            this.lbUnidad.Name = "lbUnidad";
            this.lbUnidad.Size = new System.Drawing.Size(100, 23);
            this.lbUnidad.TabIndex = 0;
            this.lbUnidad.Text = "Unidad (Lt. / Kg. /...)";
            this.lbUnidad.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbUnidad
            // 
            this.tbUnidad.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbUnidad.Location = new System.Drawing.Point(0, 0);
            this.tbUnidad.MaxLength = 10;
            this.tbUnidad.Name = "tbUnidad";
            this.tbUnidad.Size = new System.Drawing.Size(120, 30);
            this.tbUnidad.TabIndex = 0;
            // 
            // lbCodigo
            // 
            this.lbCodigo.AutoSize = true;
            this.lbCodigo.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCodigo.Location = new System.Drawing.Point(0, 0);
            this.lbCodigo.Name = "lbCodigo";
            this.lbCodigo.Size = new System.Drawing.Size(100, 23);
            this.lbCodigo.TabIndex = 0;
            this.lbCodigo.Text = "Código";
            this.lbCodigo.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbCodigo
            // 
            this.tbCodigo.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCodigo.Location = new System.Drawing.Point(0, 0);
            this.tbCodigo.MaxLength = 10;
            this.tbCodigo.Name = "tbCodigo";
            this.tbCodigo.Size = new System.Drawing.Size(120, 30);
            this.tbCodigo.TabIndex = 0;
            this.ResumeLayout(false);

        }

        private void btSubmit_Click(object sender, EventArgs e)
        {

            if (this.validarTextboxProducto())
            {
                MateriaPrima materiaPrima= new MateriaPrima();

                try
                {
                    cargarTextBoxEnMateriaPrima(materiaPrima);
                }
                catch (MiException ex)
                {
                    //  CLogger.Append("[btSubmit_Click] Exception: " + ex, CLogger.ERROR);      //DEBUG
                    MessageBox.Show("Error al dar de alta Materia Prima - Datos Incompletos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

                try
                {
                    if (handler.buscarMateriaPrimaPorNombre(materiaPrima.Nombre) == null)
                    {

                        this.handler.altaMateriaPrima(materiaPrima);
                        MessageBox.Show("Nueva Materia Prima  creada correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        limpiarTextBoxs();

                        lbIdMateriaPrima.Text = this.handler.buscarNuevoUltimoIdMateriaPrimaEnBD().ToString();


                    }
                    else
                    {
                        tbNombre.Clear();
                        MessageBox.Show("Ya existe una Materia Prima con ese nombre- Ingrese \n otro nombre de Materia Prima", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }


                }
                catch (MiException ex)
                {
                    CLogger.Append("[btSubmit_Click] Exception: " + ex, CLogger.ERROR);      //DEBUG
                    MessageBox.Show("Error al dar de alta Materi Prima- Datos Incompletos", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("Datos vacios, revisar datos necesarios de Materia Prima", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void limpiarTextBoxs()
        {
            tbNombre.Clear();
            tbDescripcion.Clear();
            tbCaracteristicas.Clear();
            tbCodigo.Clear();
            tbUnidad.Clear();

            


        }

        private bool validarTextboxProducto()
        {
            return (!string.IsNullOrWhiteSpace(lbIdMateriaPrima.Text))&& (!string.IsNullOrWhiteSpace(tbNombre.Text)) && (!string.IsNullOrWhiteSpace(tbUnidad.Text));
            //&& (!string.IsNullOrWhiteSpace(tbCodigo.Text)) && (!string.IsNullOrWhiteSpace(tbCaracteristicas.Text)) && (!string.IsNullOrWhiteSpace(tbDescripcion.Text));
        }
        private void cargarTextBoxEnMateriaPrima(MateriaPrima materiaPrima)
        {
            if (!string.IsNullOrWhiteSpace(lbIdMateriaPrima.Text)) materiaPrima.IdMateriaPrima= Convert.ToInt32(lbIdMateriaPrima.Text); else throw new Exception("Exception en ID de Materia prima");
            if (!string.IsNullOrWhiteSpace(tbNombre.Text)) materiaPrima.Nombre = tbNombre.Text; else throw new Exception("Exception en Nombre de Materia Prima");
            if (!string.IsNullOrWhiteSpace(tbDescripcion.Text)) materiaPrima.Descripcion = tbDescripcion.Text; else materiaPrima.Descripcion="";

            if (!string.IsNullOrWhiteSpace(tbCaracteristicas.Text)) materiaPrima.Caracteristicas= tbCaracteristicas.Text; else materiaPrima.Caracteristicas="";

            if (!string.IsNullOrWhiteSpace(tbUnidad.Text)) materiaPrima.Unidad = tbUnidad.Text; else throw new Exception("Exception en Unidad de Materia Prima");

            if (!string.IsNullOrWhiteSpace(tbCodigo.Text)) materiaPrima.Codigo = tbCodigo.Text; else materiaPrima.Codigo = "";

            

            //if (!string.IsNullOrWhiteSpace(dtFechaElab.Value.ToString())) materiaPrima.FechaElaboracion = dtFechaElab.Value; else materiaPrima.FechaElaboracion =  DateTime.Now;

            //if (!string.IsNullOrWhiteSpace(dtFechaVenc.Value.ToString())) materiaPrima.FechaVencimiento = dtFechaVenc.Value; else materiaPrima.FechaVencimiento = DateTime.Now.AddYears(50);

        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }
    }
}
